package com.ruoyi.financial.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.financial.domain.FEnter;
import com.ruoyi.financial.domain.FOutbound;
import com.ruoyi.financial.domain.TongJi;
import com.ruoyi.financial.service.IFEnterService;
import com.ruoyi.financial.service.IFOutboundService;
import com.ruoyi.financial.service.IFThingService;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/financial/tongji")
public class TongjiController extends BaseController {
    private String prefix = "financial/tongji";

    @Autowired
    private ISysPostService postService;

    @Autowired
    private IFThingService fThingService;

    @Autowired
    private IFEnterService fEnterService;

    @Autowired
    private IFOutboundService fOutboundService;

    @RequiresPermissions("financial:tongji:view")
    @GetMapping()
    public String fEnter(ModelMap mmap) {
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/tongji";
    }

    @PostMapping("/enterList")
    @ResponseBody
    public AjaxResult enterList(FEnter fEnter) {
        Long thingId = null;
        if (fEnter.getParams() != null && fEnter.getParams().get("thingId") != null && !fEnter.getParams().get("thingId").toString().equals("")) {
            thingId = Long.parseLong(fEnter.getParams().get("thingId").toString());
        }
        Map returnMap = new HashMap();
        if (thingId == null) {
            return AjaxResult.success("查询成功", returnMap);
        }
        fEnter.setThingId(thingId);
        List<FEnter> tongjiList = fEnterService.selectFEnterTongji(fEnter);
        String averageDanjia = fEnterService.selectAverageDanjia(fEnter);
        returnMap.put("tongjiList", tongjiList);
        returnMap.put("averageDanjia", averageDanjia);

        FOutbound fOutbound = new FOutbound();
        fOutbound.setThingId(thingId);
        fOutbound.setDeptId(fEnter.getDeptId());
        fOutbound.setThingTypeId(fEnter.getThingTypeId());
        fOutbound.setThingName(fEnter.getThingName());
        fOutbound.setCreateName(fEnter.getCreateName());
        fOutbound.setMohu(fEnter.getMohu());
        fOutbound.setParams(fEnter.getParams());
        List<FOutbound> outboundTongji = fOutboundService.selectFOutboundTongji(fOutbound);
        String averageSunhao = fOutboundService.selectAverageSunhao(fOutbound);
        returnMap.put("outboundTongji", outboundTongji);
        returnMap.put("averageSunhao", averageSunhao);

        return AjaxResult.success("查询成功", returnMap);
    }

    @PostMapping("/tongjiEnterList")
    @ResponseBody
    public TableDataInfo tongjiEnterList(FEnter fEnter) {
        startPage();
        List<TongJi> list = fEnterService.selectFEnterTongjiList(fEnter);
        return getDataTable(list);
    }

    @RequiresPermissions("financial:tongji:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FEnter fEnter) {
        List<TongJi> list = fEnterService.selectFEnterTongjiList(fEnter);
        ExcelUtil<TongJi> util = new ExcelUtil<TongJi>(TongJi.class);

        return util.exportExcel(list, "统计");
    }


}
